// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import axios from 'axios'
import VueAxios from 'vue-axios'
// import MintUI from 'mint-ui'
import 'vue-resource'
import App from './App'
import store from './store'
import router from './router'
import gobal from './global.js'
import iconSvg from '@/components/iconSvg'
import VueLazyload from 'vue-lazyload'
import { Confirm, Alert, Toast, Notify, Loading } from 'vue-ydui/dist/lib.px/dialog'

Vue.use(gobal)
Vue.use(VueLazyload)
Vue.use(VueAxios, axios)
Vue.component('icon-svg', iconSvg) // fontIcon 若在阿里图标中添加了新图标,请在index.html中更新代码
Vue.config.productionTip = false

// Vue.use(MintUI)

Vue.prototype.$dialog = {
    confirm: Confirm,
    alert: Alert,
    toast: Toast,
    notify: Notify,
    loading: Loading,
}

// 正式版
// Vue.prototype.codeHttp = 'http://www.qumengkeji.com'
// Vue.prototype.HTTP = 'http://api.qumengkeji.com/';
// axios.defaults.baseURL = 'http://api.qumengkeji.com/';

// 测试版
Vue.prototype.codeHttp = 'http://test.qumengkeji.com'
Vue.prototype.HTTP = 'http://apitest.qumengkeji.com/';
axios.defaults.baseURL = 'http://apitest.qumengkeji.com/';


router.beforeEach((to, from, next) => {

    if (to.meta.auth) {
        if (process.env.NODE_ENV == 'development') { //开发环境
            localStorage.setItem('openid', 'oU_NSwSXiJkXUrdW6jJ_7ORwVQTc')  // 束
            localStorage.setItem('uid', 120)
            next();
        } else { //上线环境
            if (to.query.openid != undefined) {
                localStorage.setItem('openid', to.query.openid)
            }

            // 判断本地是否存在openid
            if (localStorage.getItem('openid')) {
                axios.get('/user/info', {
                    params: {
                        openid: localStorage.getItem('openid')
                    }
                }).then(res => {
                    if (res.data.result) {
                        localStorage.setItem('uid', res.data.datas.id)
                        // 判断是否绑定了手机号
                        console.log(res.data.datas.mobile)
                        if (!res.data.datas.mobile) { //如果没有电话号
                            console.log(to.meta.login)
                            // 没有绑定手机号跳到登录页
                            if (to.meta.login == undefined) {
                                localStorage.removeItem('openid')
                                localStorage.removeItem('uid')
                                window.location.href = '/my/login'
                            }

                        }

                    }
                })

                next();
            } else {
                // 测试版
                window.location.href = "http://api.qumengkeji.com/wx/login/auth?page=http://test.qumengkeji.com" + to.fullPath

                // 正式版
                // window.location.href = "http://api.qumengkeji.com/wx/login/auth?page=http://www.qumengkeji.com" + to.fullPath
            }
        }
    } else {
        next();
    }

})

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    store,
    template: '<App/>',
    components: {
        App
    }
})